﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Traceability.Infrastructure
{
    public class BaseRepository<T> : IBaseRepository<T> where T : class
    {

        private readonly DataDbContext _context;

        public BaseRepository(DataDbContext context)
        {
            _context = context;
        }

        public async Task<int> Add(T entity)
        {
           await _context.Set<T>().AddAsync(entity);
            return await _context.SaveChangesAsync();
        }

        public async Task<int> Del(T entity)
        {
             _context.Set<T>().Remove(entity);
            return await _context.SaveChangesAsync();
        }

        public IQueryable<T> GetAll()
        {
            return _context.Set<T>().AsQueryable();
        }

        public async Task<int> ListAdd(List<T> entity)
        {
            await _context.Set<T>().AddRangeAsync(entity);
            return await _context.SaveChangesAsync();
        }

        public async Task<int> ListUpdate(List<T> entity)
        {
             _context.Set<T>().UpdateRange(entity);
            return await _context.SaveChangesAsync();
        }

        public async Task<int> Update(T entity)
        {
            _context.Set<T>().Update(entity);
            return await _context.SaveChangesAsync();
        }
    }
}
